package com.zzptc.zzsgz.bdsjws.activities;

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.ActionMode;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.FirstFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ThreeSosFragment;
import com.zzptc.zzsgz.bdsjws.fragments.TwoFragment;

import butterknife.BindView;

public class OneKeySOSActivity extends BaseActivity {

    @BindView(R.id.tb_soskey)
    Toolbar tbSoskey;
    @BindView(R.id.fl_soskey)
    FrameLayout flSoskey;
    FirstFragment firstFragment;
    @BindView(R.id.iv_user)
    ImageView ivUser;
    TwoFragment twoFragment;
    ThreeSosFragment threeSosFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(tbSoskey);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_one_key_sos;
    }

    @Override
    public void initData() {

    }

    @Override
    public void intView() {
        //加载Fragment
        if (firstFragment == null) {
            firstFragment = new FirstFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_soskey, firstFragment).addToBackStack("SOS").commit();
        // getFragmentManager().beginTransaction().replace(R.id.fl_soskey,firstFragment).commit();
    }

    //由第二个Fragmnet替换了第一个Fragment碎片
    public void toTwoActivity() {
        if(twoFragment==null){
            twoFragment=new TwoFragment();
        }
        getFragmentManager().beginTransaction().hide(firstFragment).add(R.id.fl_soskey,twoFragment).addToBackStack("SOS").commit();
        //让问题的图标消失  在回退时图标再显示
        ivUser.setVisibility(View.GONE);
        //getFragmentManager().beginTransaction().hide(firstFragment).add(R.id.fl_soskey,new TwoFragment()).commit();
    }
    public void ToThreeFragment(){
        if(threeSosFragment==null){
            threeSosFragment=new ThreeSosFragment();
        }
        getFragmentManager().popBackStack("sos", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        getFragmentManager().beginTransaction().hide(twoFragment).add(R.id.fl_soskey,threeSosFragment).addToBackStack("sos").commit();
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed);
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
            ivUser.setVisibility(View.VISIBLE);
        } else {
            finish();
        }
    }

    @Nullable
    @Override
    public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
        return super.onWindowStartingActionMode(callback);

    }
}
